package com.ffcs.crmd.tsp.core.util;

import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 
 * 功能说明:配置文件加载工具包
 *
 * @author ZHONGFUHUA
 * 
 * @Date 2016年1月8日 上午10:17:31
 *
 *
 * 版本号  |   作者   |  修改时间   |   修改内容
 *
 */
public class PropertiesUtil {
    
    private static PropertiesUtil propertiesUtil= new PropertiesUtil();
    
    protected PropertiesUtil() {
        
    }
    
    /**
     * 
     * 功能说明:获取
     * 
     * @author ZHONGFUHUA
     *
     * @Date 2016年2月2日
     *
     */
    public static PropertiesUtil getInstance() {
        return propertiesUtil;
    }
    
    /**
     * 
     * 功能说明:加载指定路径配置文件
     * 
     * @author ZHONGFUHUA
     * 
     * @throws IOException 
     *
     * @Date 2016年1月8日
     *
     */
    public Properties load(String fileName) {
        Properties properties = new Properties();
        InputStream inputStream = PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName);
        try {
            properties.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                if(inputStream != null){
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return properties;
    }
    
    
    public Properties loadWeb(String fileName) {
        Properties properties = new Properties();
        String classPath = Thread.currentThread().getContextClassLoader().getResource("/").getPath();
        String classRootPath = classPath.substring(0, classPath.indexOf("WEB-INF"));
        String clusterPath = classRootPath + fileName;
        clusterPath = clusterPath.replaceAll("%20", " ");
        FileReader reader = null;
        try {
            reader = new FileReader(clusterPath);
            properties.load(reader);
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                if(reader != null){
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return properties;
    }
    
    public String getWebPath(String fileName) {
        String classPath = Thread.currentThread().getContextClassLoader().getResource("/").getPath();
        String classRootPath = classPath.substring(0, classPath.indexOf("WEB-INF"));
        String clusterPath = classRootPath + fileName;
        clusterPath = clusterPath.replaceAll("%20", " ");
        return clusterPath;
    }
}
